package com.example.biji.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.biji.adapter.NoteAdapter;
import com.example.biji.dao.NoteDao;
import com.example.biji.model.Note;
import com.example.biji.R;

import java.util.List;

public class NoteSearchActivity extends AppCompatActivity {
    private EditText etSearch;
    private ImageView ivSearch, ivBack;
    private RecyclerView rvSearchResults;
    private TextView tvEmptyResult;
    private NoteAdapter noteAdapter;
    private List<Note> searchResults;
    private NoteDao noteDao;
    private int userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_search);

        // 获取传递的用户ID
        userId = getIntent().getIntExtra("userId", -1);
        if (userId == -1) {
            Toast.makeText(this, "用户ID获取失败", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 初始化视图和监听器
        initViews();
        setupListeners();
    }

    private void initViews() {
        etSearch = findViewById(R.id.et_search);
        ivSearch = findViewById(R.id.iv_search);
        ivBack = findViewById(R.id.iv_back);
        rvSearchResults = findViewById(R.id.rv_search_results);
        tvEmptyResult = findViewById(R.id.tv_empty_result);
        noteDao = new NoteDao(this);

        // 设置RecyclerView
        rvSearchResults.setLayoutManager(new LinearLayoutManager(this));
        rvSearchResults.setHasFixedSize(true);
    }

    private void setupListeners() {
        // 返回按钮点击事件
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed(); // 调用系统返回逻辑
            }
        });

        // 搜索按钮点击事件
        ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                performSearch();
            }
        });
    }

    private void performSearch() {
        String keyword = etSearch.getText().toString().trim();

        if (TextUtils.isEmpty(keyword)) {
            Toast.makeText(this, "请输入搜索关键词", Toast.LENGTH_SHORT).show();
            return;
        }

        // 执行数据库搜索
        searchResults = noteDao.searchNotes(userId, keyword);

        if (searchResults.isEmpty()) {
            // 无搜索结果
            tvEmptyResult.setVisibility(View.VISIBLE);
            rvSearchResults.setVisibility(View.GONE);
        } else {
            // 显示搜索结果
            tvEmptyResult.setVisibility(View.GONE);
            rvSearchResults.setVisibility(View.VISIBLE);

            // 设置适配器
            noteAdapter = new NoteAdapter(searchResults);
            noteAdapter.setOnItemClickListener(new NoteAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    // 点击笔记项，跳转到详情页
                    Note note = searchResults.get(position);
                    Intent intent = new Intent(NoteSearchActivity.this, NoteDetailActivity.class);
                    intent.putExtra("noteId", note.getId());
                    intent.putExtra("userId", userId);
                    startActivity(intent);
                }
            });
            rvSearchResults.setAdapter(noteAdapter);
        }
    }
}